package com.sz.biz.common.project.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.project.entity.ProjectQuestion;
import com.sz.biz.common.project.service.ProjectQuestionService;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.service.PrincipalUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: TODO: ADD FUNCTION <br>
 * Author: qiankun<br>
 * Date: 2017-07-13 10:30:00
 */
@Service("ProjectQuestionService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProjectQuestionServiceImpl extends AbstractService implements ProjectQuestionService {

    @Override
    public void deleteByProjectId(int projectId) {
        if (projectId != 0) {
            dao.update("ProjectQuestionMapper.deleteByProject", projectId);
        }
    }

    @Override
    public void delete(int id) {
        if (id != 0) {
            dao.update("ProjectQuestionMapper.deleteById", id);
        }
    }

    @Override
    public int addQuestion(ProjectQuestion projectQuestion) {
        if(projectQuestion.getQuestionId()!=null&&projectQuestion.getQuestionId()!=0){
            ProjectQuestion exist =
                    (ProjectQuestion)dao.findForObject("ProjectQuestionMapper.selectByPrimaryKey",projectQuestion.getQuestionId());
            projectQuestion.setType(exist.getType());
        }
        checkQuestionData(projectQuestion);
        projectQuestion.setUserId(PrincipalUtils.getAccountId());
        dao.save("ProjectQuestionMapper.saveAskQuestion", projectQuestion);
        if(projectQuestion.getQuestionId() == null ||projectQuestion.getQuestionId()==0){
            dao.update("ProjectQuestionMapper.updateQuestionIdById",projectQuestion.getId());
        }
        return projectQuestion.getId();
    }

    private void checkQuestionData(ProjectQuestion projectQuestion) {
        if (projectQuestion != null) {
            if (StringUtils.isEmpty(projectQuestion.getContent())  ) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "content");
            }
            if (StringUtils.isEmpty(projectQuestion.getProjectId()) || projectQuestion.getProjectId() == 0) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
            }
            if (StringUtils.isEmpty(projectQuestion.getType()) || projectQuestion.getType() == 0) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "type");
            }
        }
    }


    @Override
    public List<ProjectQuestion> queryByProjectIdAndType(int projectId, int type) {
        if (projectId == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
        }
        if (type == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "type");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("projectId", projectId);
        map.put("type", type);
        return dao.findForList("ProjectQuestionMapper.findByProjectIdAndType", map, ProjectQuestion.class);
    }


}
